package com.byx.chatclient.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.bean.ChildInfo;
import com.byx.chatclient.bean.FriendInfo;
import com.byx.chatclient.bean.GroupInfo;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * FriendsListAdapter
 *
 * @author Superb
 * @date 2021/3/26 19:32
 */
public class FriendsListAdapter extends BaseExpandableListAdapter {
    public static final int GROUP_LAYOUT_RES_ID = R.layout.fragment_contact_people_friend_item_group_view;
    public static final int CHILD_LAYOUT_RES_ID = R.layout.fragment_contact_people_friend_item_child_view;
    private static final int BG_PUT_AWAY_TRIANGLE_RES_ID = R.mipmap.ic_triangle_gray_48;
    private static final int BG_EXPAND_TRIANGLE_RES_ID = R.mipmap.ic_triangle_expand_gray_48;

    private final Context mContext;
    private List<GroupInfo> mFriends;

    public FriendsListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public FriendsListAdapter(Context mContext, List<GroupInfo> mFriends) {
        this.mContext = mContext;
        this.mFriends = mFriends;
    }

    public void setFriends(List<GroupInfo> friends) {
        this.mFriends = friends;
    }

    /**
     * Gets the number of groups.
     *
     * @return the number of groups
     */
    @Override
    public int getGroupCount() {
        return mFriends.size();
    }

    /**
     * Gets the number of children in a specified group.
     *
     * @param groupPosition the position of the group for which the children
     *                      count should be returned
     * @return the children count in the specified group
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        return mFriends.get(groupPosition).getChildInfoList().size();
    }

    /**
     * Gets the data associated with the given group.
     *
     * @param groupPosition the position of the group
     * @return the data child for the specified group
     */
    @Override
    public Object getGroup(int groupPosition) {
        return mFriends.get(groupPosition);
    }

    /**
     * Gets the data associated with the given child within the given group.
     *
     * @param groupPosition the position of the group that the child resides in
     * @param childPosition the position of the child with respect to other
     *                      children in the group
     * @return the data of the child
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mFriends.get(groupPosition).getChildInfoList().get(childPosition);
    }

    /**
     * Gets the ID for the group at the given position. This group ID must be
     * unique across groups. The combined ID (see
     * {@link #getCombinedGroupId(long)}) must be unique across ALL items
     * (groups and all children).
     *
     * @param groupPosition the position of the group for which the ID is wanted
     * @return the ID associated with the group
     */
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    /**
     * Gets the ID for the given child within the given group. This ID must be
     * unique across all children within the group. The combined ID (see
     * {@link #getCombinedChildId(long, long)}) must be unique across ALL items
     * (groups and all children).
     *
     * @param groupPosition the position of the group that contains the child
     * @param childPosition the position of the child within the group for which
     *                      the ID is wanted
     * @return the ID associated with the child
     */
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return Long.parseLong(((FriendInfo) mFriends.get(groupPosition).getChildInfoList().get(childPosition)).getLoginId());
    }

    /**
     * Indicates whether the child and group IDs are stable across changes to the
     * underlying data.
     *
     * @return whether or not the same ID always refers to the same object
     * @see Adapter#hasStableIds()
     */
    @Override
    public boolean hasStableIds() {
        return true;
    }

    /**
     * Gets a View that displays the given group. This View is only for the
     * group--the Views for the group's children will be fetched using
     * {@link #getChildView(int, int, boolean, View, ViewGroup)}.
     *
     * @param groupPosition the position of the group for which the View is
     *                      returned
     * @param isExpanded    whether the group is expanded or collapsed
     * @param convertView   the old view to reuse, if possible. You should check
     *                      that this view is non-null and of an appropriate type before
     *                      using. If it is not possible to convert this view to display
     *                      the correct data, this method can create a new view. It is not
     *                      guaranteed that the convertView will have been previously
     *                      created by
     *                      {@link #getGroupView(int, boolean, View, ViewGroup)}.
     * @param parent        the parent that this view will eventually be attached to
     * @return the View corresponding to the group at the specified position
     */
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupInfo groupInfo = mFriends.get(groupPosition);
        GroupViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(GROUP_LAYOUT_RES_ID,parent,false);

            viewHolder.mIvTriangle = convertView.findViewById(R.id.iv_triangle);
            viewHolder.mTvGroupName = convertView.findViewById(R.id.tv_group_name);
            viewHolder.mTvChildCount = convertView.findViewById(R.id.tv_child_view_count);

            convertView.setTag(viewHolder);
            //将当前groupPosition设置到tag中，可在长按时获取
            convertView.setTag(GROUP_LAYOUT_RES_ID,groupPosition);
            convertView.setTag(CHILD_LAYOUT_RES_ID,-1);
        }

        viewHolder = (GroupViewHolder) convertView.getTag();
        viewHolder.mIvTriangle.setImageResource(isExpanded ? BG_EXPAND_TRIANGLE_RES_ID : BG_PUT_AWAY_TRIANGLE_RES_ID);
        viewHolder.mTvGroupName.setText(groupInfo.getGroupName());
        viewHolder.mTvChildCount.setText(String.format("%s/%s",1,groupInfo.getChildInfoList().size()));

        return convertView;
    }

    /**
     * Gets a View that displays the data for the given child within the given
     * group.
     *
     * @param groupPosition the position of the group that contains the child
     * @param childPosition the position of the child (for which the View is
     *                      returned) within the group
     * @param isLastChild   Whether the child is the last child within the group
     * @param convertView   the old view to reuse, if possible. You should check
     *                      that this view is non-null and of an appropriate type before
     *                      using. If it is not possible to convert this view to display
     *                      the correct data, this method can create a new view. It is not
     *                      guaranteed that the convertView will have been previously
     *                      created by
     *                      {@link #getChildView(int, int, boolean, View, ViewGroup)}.
     * @param parent        the parent that this view will eventually be attached to
     * @return the View corresponding to the child at the specified position
     */
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        FriendInfo friendInfo = ((FriendInfo) mFriends.get(groupPosition).getChildInfoList().get(childPosition));
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(CHILD_LAYOUT_RES_ID,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.mAvatar = convertView.findViewById(R.id.avatar);
            childViewHolder.mTvUserName = convertView.findViewById(R.id.tv_friend_username);
            childViewHolder.mTvUserStatus = convertView.findViewById(R.id.tv_friend_status);
            convertView.setTag(childViewHolder);

            //将groupPosition和childPosition都设置到tag中
            convertView.setTag(GROUP_LAYOUT_RES_ID,groupPosition);
            convertView.setTag(CHILD_LAYOUT_RES_ID,childPosition);
        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }

        childViewHolder.mAvatar.setImageURI(friendInfo.getAvatar());
        childViewHolder.mTvUserName.setText(friendInfo.getName());
        childViewHolder.mTvUserStatus.setText(friendInfo.getStatus() == 200 ? "在线" : "离线");

        return convertView;
    }

    /**
     * Whether the child at the specified position is selectable.
     *
     * @param groupPosition the position of the group that contains the child
     * @param childPosition the position of the child within the group
     * @return whether the child is selectable.
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static final class GroupViewHolder{
        protected ImageView mIvTriangle;
        protected TextView mTvGroupName;
        protected TextView mTvChildCount;
    }

    private static final class ChildViewHolder {
        protected SimpleDraweeView mAvatar;
        protected TextView mTvUserName;
        protected TextView mTvUserStatus;
    }
}
